package com.ie.service.admin;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ie.common.http.ResponseResult;
import com.ie.common.security.utiles.ConfigrarionBean;
import com.ie.common.security.utiles.HttpJwtTokenUtil;
import com.ie.common.security.utiles.JwtTokenUtil;
import com.ie.common.security.utiles.RedisUtil;
import com.ie.entity.AUserEntity;
import com.ie.mapper.admin.AUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; // 导入 @Service 注解

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service // 添加 @Service 注解
public class AUserServiceImpl extends ServiceImpl<AUserMapper, AUserEntity> implements AUserService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ConfigrarionBean config;
    //对传入的对象进行比较
    //1.成功有这个个用户:a.密码正确;b.密码错误
    //用户不存在
    //2.密码正确：返回token，跳转对应的展示网页
    @Override
    public ResponseResult<Map<String, String>> login(AUserEntity aUserEntity) {
//读取这个参数
        Long count =lambdaQuery().eq(AUserEntity::getAname,aUserEntity.getAname()).count();
        if(count == 0){
            return ResponseResult.getMessageResult(null,"A001");
        }
        HashMap<String, String> mapuser =new HashMap<>();
        mapuser.put("aname",aUserEntity.getAname());
        mapuser.put("apwd",aUserEntity.getApwd());
        List<AUserEntity> userlist=query().allEq(mapuser).list();
        if(userlist.size() > 0){
            String token=jwtTokenUtil.createToken(aUserEntity.getAname());
            redisUtil.set("login"+aUserEntity.getAname(),aUserEntity.getAname(),config.getRedisExpiration());
            HashMap<String, String> loginresult =new HashMap<>();
            loginresult.put("authtoken",token);
            loginresult.put("aname",aUserEntity.getAname());
            loginresult.put("aid",aUserEntity.getId()+"");
            return ResponseResult.getSuccessResult(loginresult);
        } else {
            return ResponseResult.getMessageResult(null,"A002");
        }
    }
}
